<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/17
 * Time: 10:43
 */

namespace app\admin\controller\mall;


use app\admin\BaseController;
use app\lib\exception\FailedException;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use app\Validate\ProductCategoryAddValidate;
use think\Request;

class ProductCategory extends BaseController
{

    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    //查询三级分类数据
    public function categoryList(Request $request){
        (new PageMustValidate())->goCheck();
        $where = [];
        $post  = $request->post();
//        $where[] = ['uid','eq',$post['uid']];
        $where = addWhere($post,['name'=>''],$where,2);//关键字模糊搜索
        $where = addWhere($post,['level'=>'','pid'=>''],$where,1);//等级 和 上一级的分类
        $count  = (new \app\admin\model\ProductCategory())->getCount($where);
        $where['page'] = $post['page'];
        $dataList =  (new \app\admin\model\ProductCategory())->wechatList($where,"id,pid,sort,name,cover_img,level,create_time","sort desc,id desc",$post['page_number']);
        return writeJson(200,['data'=>$dataList,'count'=>$count]);
    }

    /**
     * 添加 分类
     * 添加的时候不需要id  修改的时候需要id
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function categoryAdd(Request $request){
        (new ProductCategoryAddValidate())->goCheck();
        $post  = $request->post();
        $new = [
//            'uid' =>$post['uid'],
            'cover_img'=>$post['cover_img'],
            'sort'=>$post['sort'],
            'pid'=>$post['pid'],
            'level'=>$post['level'],
            'name' =>$post['name']
        ];
        $result = (new \app\admin\model\ProductCategory())->dataUpdate($new);
        if(!$result){
            if(isset($post['id'])){
                throw new FailedException([ 'msg' => '修改分类失败，请重试']);
            }else{
                throw new FailedException([ 'msg' => '新增分类失败，请重试']);
            }
        }
        return writeJson(200,[]);
    }
    /**
     * 修改 分类
     *  修改的时候需要id
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function categoryEdit(Request $request){
        (new ProductCategoryAddValidate())->goCheck();
        (new IdMustPositive())->goCheck();
        $post  = $request->post();
        $result = (new \app\admin\model\ProductCategory())->dataUpdate($post);
        if(!$result){
            throw new FailedException([ 'msg' => '修改分类失败，请重试']);
        }
        return writeJson(200,[]);
    }
    /**
     * 删除 分类
     *  修改的时候需要id
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function categoryDelete(Request $request){
        (new IdMustPositive())->goCheck();
        $post  = $request->post();
        $result = (new \app\admin\model\ProductCategory())->SoftDelete([['id','eq',$post['id']]]);
        if(!$result){
            throw new FailedException([ 'msg' => '删除分类失败，请重试']);
        }
        return writeJson(200,[]);
    }
}
